package com.my.mongodbspringdemo.contorller;

import com.my.mongodbspringdemo.entity.Article;
import com.my.mongodbspringdemo.entity.CIForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Description
 * @Author Godzilla
 * @Date 2019/7/6
 * @Version V1.0.0
 * @Update 更新说明
 */
@RestController
@RequestMapping("/mongodb-demo")
public class MongodbContorller{

    @Autowired
    MongoTemplate mongoTemplate;

    /**
     * 添加
     * @return
     */
    @RequestMapping("add")
    public String add() {
        //循环添加
        for (int i = 0; i < 10; i++) {
            Article article = new Article();
            article.setTitle("MongoTemplate的基本使用");
            article.setAuthor("yinjihuan");
            article.setUrl("http://cxytiandi.com/blog/detail/" + i);
            article.setTags(Arrays.asList("java", "mongodb", "spring"));
            article.setVisitCount(0L);
            article.setAddTime(new Date());
            mongoTemplate.save(article);
        }

        //批量添加
        List<Article> articles = new ArrayList<>(10);
        for (int i = 0; i < 10; i++) {
            Article article = new Article();
            article.setTitle("MongoTemplate的基本使用");
            article.setAuthor("yinjihuan");
            article.setUrl("http://cxytiandi.com/blog/detail/" + i);
            article.setTags(Arrays.asList("java", "mongodb", "spring"));
            article.setVisitCount(0L);
            article.setAddTime(new Date());
            articles.add(article);
        }
        mongoTemplate.insert(articles, Article.class);
        return "success";
    }

    /**
     * 删除
     * @return
     */
    @RequestMapping("delete")
    public String delete() {
        //删除author为yinjihuan的数据
        Query query = Query.query(Criteria.where("author").is("yinjihuan"));
        mongoTemplate.remove(query, Article.class);
        //如果实体类中没配集合名词，可在删除的时候单独指定article_info
        query = Query.query(Criteria.where("author").is("yinjihuan"));
        mongoTemplate.remove(query, "article_info");
        //删除集合，可传实体类，也可以传名称
        mongoTemplate.dropCollection(Article.class);
        mongoTemplate.dropCollection("article_info");
        //复制代码下面这2种适合要知道删除的数据有哪些的场景，比如记录删除的记录
        //查询出符合条件的第一个结果，并将符合条件的数据删除,只会删除第一条
        query = Query.query(Criteria.where("author").is("yinjihuan"));
        Article article = mongoTemplate.findAndRemove(query, Article.class);
        //查询出符合条件的所有结果，并将符合条件的所有数据删除
        query = Query.query(Criteria.where("author").is("yinjihuan"));
        List<Article> articles = mongoTemplate.findAllAndRemove(query, Article.class);
        return "success";
    }

    /**
     * 局部更新
     * @return
     */
    @RequestMapping("update")
    public String update() {
        //修改第一条author为yinjihuan的数据中的title和visitCount
        Query query = Query.query(Criteria.where("author").is("yinjihuan"));
        Update update = Update.update("title", "MongoTemplate").set("visitCount", 10);
        mongoTemplate.updateFirst(query, update, Article.class);
        //修改全部符合条件的
        query = Query.query(Criteria.where("author").is("yinjihuan"));
        update = Update.update("title", "MongoTemplate").set("visitCount", 10);
        mongoTemplate.updateMulti(query, update, Article.class);
        return "success";
    }
    /**
     * 查询
     * @return
     */
    @RequestMapping("query")
    public List<CIForm> query() {
        //根据作者查询所有符合条件的数据，返回List

        List<CIForm> articles;

        //查询集合中所有数据，不加条件
        articles = mongoTemplate.findAll(CIForm.class);

        return articles;
    }


}
